﻿using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;

namespace Streambolics
{
    public static class FileInfoExtensions
    {
        [DllImport ("kernel32.dll", CharSet = CharSet.Auto)]
        private static extern int GetShortPathName ([MarshalAs (UnmanagedType.LPTStr)] string path,
                                                    [MarshalAs (UnmanagedType.LPTStr)] StringBuilder shortPath,
                                                    int shortPathLength);

        /// <summary>
        ///     Appends all text to the file, create it if necessary.
        /// </summary>
        /// <param name="f">
        ///     The file to write to.
        /// </param>
        /// <param name="aText">
        ///     The text to append.
        /// </param>

        public static void AppendAllText (this FileInfo f, string aText)
        {
            File.AppendAllText (f.FullName, aText);
        }

        /// <summary>
        ///     Edit the file using the registered editor.
        /// </summary>
        /// <param name="f">
        ///     The file to edit.
        /// </param>

        public static void Edit (this FileInfo f)
        {
            Process.Start (f.FullName);
        }

        /// <summary>
        ///     Edit the file using the registered editor, requesting
        ///     elevated privileges.
        /// </summary>
        /// <param name="f">
        ///     The file to edit.
        /// </param>

        public static void EditElevated (this FileInfo f)
        {
            if (WindowsUser.HasAdministrativeRights)
            {
                f.Edit ();
            }
            else
            {
                ProcessStartInfo processInfo = new ProcessStartInfo ();
                processInfo.Verb = "runas";
                processInfo.FileName = f.FullName;
                Process.Start (processInfo);
            }
        }

        public static void EditText (this FileInfo f)
        {
            Process.Start ("notepad.exe", f.FullName);
        }

        public static void EditTextElevated (this FileInfo f)
        {
            if (WindowsUser.HasAdministrativeRights)
            {
                f.EditText ();
            }
            else
            {
                ProcessStartInfo processInfo = new ProcessStartInfo ();
                processInfo.Verb = "runas";
                processInfo.FileName = "notepad.exe";
                processInfo.Arguments = f.FullName;
                Process.Start (processInfo);
            }
        }

        public static string ShortFileName (this FileInfo f)
        {
            StringBuilder b = new StringBuilder (255);
            GetShortPathName (f.FullName, b, 255);
            return b.ToString ();
        }

        public static string RelativePathFrom (this FileInfo f, DirectoryInfo d)
        {
            if (f.FullName.ToUpper ().StartsWith (d.FullName.ToUpper ()))
            {
                return f.FullName.Substring (d.FullName.Length);
            }
            else
            {
                return f.FullName;
            }
        }

    }
}
